package com.hmdp.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmdp.interceptor.LoginInterceptor;
import com.hmdp.interceptor.RefressTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //序列化器
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截需要登录的路径
        /*registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(
                        "/user/login",
                        "/user/code",
                        "/shop/**",
                        "/voucher/**",
                        "/upload/**",
                        "/blog/hot")
                .order(1);  //优先级默认都是0，值越大优先级越低*/

        //拦截注册表中添加登录拦截器
        registry.addInterceptor(new LoginInterceptor(stringRedisTemplate,objectMapper))
                .excludePathPatterns(
                        "/user/login",
                        "/user/code",
                        "/shop/**",
                        "/voucher/**",
                        "/upload/**",
                        "/blog/hot")
                .order(1);  //优先级默认都是0，值越大优先级越低

        //拦截一切路径，用于刷新已经登陆用户的token
        registry.addInterceptor(new RefressTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**")
                .order(0);
    }
}
